<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\admin\controller;

use app\admin\model\RoleModel;
use api\admin\controller\RoleController;

/**
 * Description of MetaRoleController
 *
 * @author PC
 */
class MetaRoleController extends RoleController
{
    /**
     * 添加角色
     * @throws \think\exception\DbException
     * @OA\Post(
     *     tags={"admin"},
     *     path="/admin/MetaRole/roleAdd",
     *     summary="添加角色",
     *     description="添加角色",
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminRoleSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminRoleSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "success","data":{
     *              "item":{"name": "财务","type": "1","remark": "角色描述","status": "1","id": 3}
     *          }})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function roleAdd()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'role');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                // 判断是否存在相同的角色
                $role = RoleModel::where('name', $data['name'])->find();
                if (!empty($role)) {
                    $this->error('角色名不能重复!');
                }

                $role = RoleModel::where('code', $data['code'])->find();
                if (!empty($role)) {
                    $this->error('角色编码不能重复!');
                }

                $result = RoleModel::create($data);
                if ($result) {
                    $this->success("成功，点击角色管理中的蓝色设置按钮进行权限设置！", ['item' => $result]);
                } else {
                    $this->error(lang('ADD_FAILED'));
                }

            }
        }
    }

    /**
     * 编辑角色
     * @throws \think\exception\DbException
     * @OA\Put(
     *     tags={"admin"},
     *     path="/admin/MetaRole/roleEdit/{id}",
     *     summary="编辑角色",
     *     description="编辑角色",
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="角色id",
     *         required=true,
     *         @OA\Schema(
     *             type="integer",
     *         )
     *     ),
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminRoleSaveRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminRoleSaveRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "保存成功","data":""})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function roleEdit($id)
    {
        $id = $this->request->param("id", 0, 'intval');
        if ($id == 1) {
            $this->error("超级管理员角色不能被修改！");
        }
        if ($this->request->isPut()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'role');
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                // 判断是否存在相同的角色
                $role = RoleModel::where('name', $data['name'])->find();
                if (!empty($role)) {
                    $this->error('角色名不能重复!');
                }

                $role = RoleModel::where('code', $data['code'])->find();
                if (!empty($role)) {
                    $this->error('角色编码不能重复!');
                }

                $role = RoleModel::find($id);
                if (empty($role)) {
                    $this->error('角色不存在!');
                }

                $role->save($data);
                $this->success(lang('EDIT_SUCCESS'));
            }
        }
    }

}
